using System;
using System.Collections;
using System.Collections.Specialized;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.DirectoryServices;
using Dimok.User;
using Dimok.Data;

namespace Dimok.Images
{
	/// <summary>
	/// Summary description for MailSelect.
	/// </summary>
	public partial class MailSelect : System.Web.UI.Page{
		protected System.Web.UI.WebControls.Literal ltrDomain;
		protected System.Web.UI.WebControls.ListBox selEMail;

		////////////////////////////////////////////////////////////////////////////////////////////////////
		/// <summary>	Event handler. Called by Page for load events. </summary>
		///
		/// <remarks>	Dimon, 12/21/2010. </remarks>
		///
		/// <param name="sender">	Source of the event. </param>
		/// <param name="e">			Event information. </param>
		////////////////////////////////////////////////////////////////////////////////////////////////////

		private void Page_Load(object sender, System.EventArgs e){
			if( ADs.isInDomain(UserObject.CurrentUserPath) ){
				string[] props = {"displayName","mail"};
				DirectorySearcher ds = new DirectorySearcher("(|(objectCategory=person)(objectCategory=publicFOlder)(objectCategory=group))",props, SearchScope.Subtree);
				SearchResultCollection src = ds.FindAll();
				selEMail.DataTextField = "text";
				selEMail.DataValueField = "value";
				selEMail.DataSource = InitTable(src);
				selEMail.DataBind();
			}
		}

		////////////////////////////////////////////////////////////////////////////////////////////////////
		/// <summary>	Initialises the table. </summary>
		///
		/// <remarks>	Dimon, 12/21/2010. </remarks>
		///
		/// <param name="src">	Source for the. </param>
		///
		/// <returns>	. </returns>
		////////////////////////////////////////////////////////////////////////////////////////////////////

		private DataView InitTable(SearchResultCollection src){
			DataTable dt = new DataTable();
			dt.Columns.Add("text",Type.GetType("System.String"));
			dt.Columns.Add("value",Type.GetType("System.String"));
			foreach( SearchResult sr in src )
				if (sr.Properties["displayName"].Count > 0 && sr.Properties["mail"].Count > 0) 
					dt.Rows.Add(sr.Properties["displayName"][0], sr.Properties["mail"][0]);
			DataView dv = dt.DefaultView;
			dv.Sort = "text";
			return dv;
		}
		#region Web Form Designer generated code

		////////////////////////////////////////////////////////////////////////////////////////////////////
		/// <summary>	Raises the initialise event. </summary>
		///
		/// <remarks>	Dimon, 12/21/2010. </remarks>
		///
		/// <param name="e">	Event information to send to registered event handlers. </param>
		////////////////////////////////////////////////////////////////////////////////////////////////////

		override protected void OnInit(EventArgs e)
		{
			//
			// CODEGEN: This call is required by the ASP.NET Web Form Designer.
			//
			InitializeComponent();
			base.OnInit(e);
		}

		////////////////////////////////////////////////////////////////////////////////////////////////////
		/// <summary>	
		/// Required method for Designer support - do not modify the contents of this method with the code
		/// editor. 
		/// </summary>
		///
		/// <remarks>	Dimon, 12/21/2010. </remarks>
		////////////////////////////////////////////////////////////////////////////////////////////////////

		private void InitializeComponent()
		{    
			this.Load += new System.EventHandler(this.Page_Load);

		}
		#endregion
	}
}
